package org.yoao.crm.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.yoao.crm.business.impl.SignupServiceImpl;
import org.yoao.crm.business.service.ISignupService;
import org.yoao.crm.page.SignupPage;

/**
 * MyEclipse Struts
 * Creation date: 08-13-2010
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class ChildsAction extends BaseAction {

	private ISignupService signupService = new SignupServiceImpl();

	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String searchName = request.getParameter("searchName");
			String searchAddress = request.getParameter("searchAddress");
			String searchTime = request.getParameter("searchTime");
			String searchIsFlag = request.getParameter("searchIsFlag");
			String searchPhone = request.getParameter("searchPhone");

			request.setAttribute("searchName", searchName);
			request.setAttribute("searchAddress", searchAddress);
			request.setAttribute("searchTime", searchTime);
			request.setAttribute("searchIsFlag", searchIsFlag);
			request.setAttribute("searchPhone", searchPhone);

			Map map = new HashMap();
			map.put("TABLENAME", "childs");
			if(searchName != null && !searchName.trim().equals("")){
				map.put("[NAME]", searchName);
			}
			if(searchAddress != null && !searchAddress.trim().equals("")){
				map.put("ADDRESS", searchAddress);
			}
			if(searchTime != null && !searchTime.trim().equals("")){
				map.put("BMTIME LIKE ", searchTime);
			}
			if(searchIsFlag != null && !searchIsFlag.trim().equals("")){
				map.put("isFlag ", searchIsFlag);
			}
			if(searchPhone != null && !searchPhone.trim().equals("")){
				map.put("phone ", searchPhone);
			}

			List dataList = getPage(request, new SignupPage(), map);

			request.setAttribute("SIGNUPLIST", dataList);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return mapping.findForward("to_view");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		String result = null;
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			String id = request.getParameter("id");

			if(signupService.delete(id, "childs"))
				result = "<script>window.close();opener.location.reload();</script>";
			else
				result = "<script>alert('删除失败:');window.close();</script>";
		} catch (IOException e) {
			result = "<script>alert('删除失败:');window.close();</script>";
			e.printStackTrace();
		} finally{
			out.println(result);
			out.flush();
			out.close();
			return null;
		}
	}

	public ActionForward signature(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		String result = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			String value = request.getParameter("v");
			String id = request.getParameter("id");
			if(id != null && !id.trim().equals("")){
				signupService.signature("childs", id, value);
			}
			result = "<script>window.close();opener.location.reload();</script>";
		} catch (Exception e) {
			e.printStackTrace();
			result = "<script>alert('操作失败:');window.close();</script>";
		} finally{
			out.println(result);
			out.flush();
			out.close();
			return null;
		}
	}

	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		String result = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			String memo = request.getParameter("memo");
			String id = request.getParameter("id");
			if(id != null && !id.trim().equals("")){
				signupService.modify("childs", id, memo);
			}
			result = "<script>window.close();opener.location.reload();</script>";
		} catch (Exception e) {
			e.printStackTrace();
			result = "<script>alert('操作失败:');window.close();</script>";
		} finally{
			out.println(result);
			out.flush();
			out.close();
			return null;
		}
	}
}